package com.andatsoft.laisim.activity.setting;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import com.andatsoft.laisim.R;
import com.andatsoft.laisim.activity.BaseActivity;
import com.andatsoft.laisim.asynctask.AlbumArtGettingTask;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.Utils;

public class AlbumArtSettingActivity extends BaseActivity {

	private CheckBox mChkBoxAutoDownload;
	private CheckBox mChkBoxDownloadWifiOnly;
	private CheckBox mChkBoxDownloadHighQuality;
	private CheckBox mChkBoxDisplayDisplayAlbum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_album_art);
		initViews();
		setupListeners();
	}

	private void initViews() {
		mChkBoxAutoDownload = (CheckBox) findViewById(R.id.chk_box_album_art_auto_download);
		mChkBoxDownloadWifiOnly = (CheckBox) findViewById(R.id.chk_box_album_art_download_on_wifi_only);
		mChkBoxDownloadHighQuality = (CheckBox) findViewById(R.id.chk_box_album_art_download_high_quality);
		mChkBoxDisplayDisplayAlbum = (CheckBox) findViewById(R.id.chk_box_album_art_display_display);
		restoreUIFromSetting();
	}

	private void restoreUIFromSetting() {
		SharedPreferences sp = getSharedPreferences(
				Common.SP_ALBUM_ART_SETTING_NAME, Context.MODE_PRIVATE);
		if (sp.getBoolean(Common.SP_ALBUM_ART_IS_DOWNLOAD, false)) {
			mChkBoxAutoDownload.setChecked(true);
		} else {
			mChkBoxAutoDownload.setChecked(false);
		}
		if (sp.getBoolean(Common.SP_ALBUM_ART_IS_DOWNLOAD_WIFI_ONLY, false)) {
			mChkBoxDownloadWifiOnly.setChecked(true);
		} else {
			mChkBoxDownloadWifiOnly.setChecked(false);
		}
		if (sp.getBoolean(Common.SP_ALBUM_ART_IS_DOWNLOAD_HIGH_QUALITY, false)) {
			mChkBoxDownloadHighQuality.setChecked(true);
		} else {
			mChkBoxDownloadHighQuality.setChecked(false);
		}
		if (sp.getBoolean(Common.SP_ALBUM_ART_IS_DISPLAY_ALBUM_ART, true)) {
			mChkBoxDisplayDisplayAlbum.setChecked(true);
		} else {
			mChkBoxDisplayDisplayAlbum.setChecked(false);
		}
		updateLayoutWhenAutoDownChanged(mChkBoxAutoDownload.isChecked());

	}

	private void updateLayoutWhenAutoDownChanged(boolean isCheck) {
		if (isCheck) {
			// check for the wifi only options
			if (mChkBoxDownloadWifiOnly.isChecked()) {
				setAlpha(
						((LinearLayout) findViewById(R.id.layout_setting_album_art_download_on_wifi_only)),
						1.0f);
			} else {
				setAlpha(
						((LinearLayout) findViewById(R.id.layout_setting_album_art_download_on_wifi_only)),
						0.5f);
				
			}

			// check for the wifi only options
			if (mChkBoxDownloadHighQuality.isChecked()) {
				setAlpha(
						((LinearLayout) findViewById(R.id.layout_setting_album_art_download_high_quality)),
						1.0f);
				
			} else {
				setAlpha(
						((LinearLayout) findViewById(R.id.layout_setting_album_art_download_high_quality)),
						0.5f);
			}
			mChkBoxDownloadHighQuality.setEnabled(true);
			mChkBoxDownloadWifiOnly.setEnabled(true);

		} else {
			// disable other options
			setAlpha(
					((LinearLayout) findViewById(R.id.layout_setting_album_art_download_on_wifi_only)),
					0.5f);
			setAlpha(
					((LinearLayout) findViewById(R.id.layout_setting_album_art_download_high_quality)),
					0.5f);
			mChkBoxDownloadHighQuality.setEnabled(false);
			mChkBoxDownloadWifiOnly.setEnabled(false);
		}
	}

	private void setupListeners() {
		mChkBoxAutoDownload.setOnClickListener(new OnClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				// save setting value
				SharedPreferences sp = getSharedPreferences(
						Common.SP_ALBUM_ART_SETTING_NAME, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = sp.edit();

				editor.putBoolean(Common.SP_ALBUM_ART_IS_DOWNLOAD,
						mChkBoxAutoDownload.isChecked());
				editor.putBoolean(Common.SP_ALBUM_ART_IS_DOWNLOAD_WIFI_ONLY,
						mChkBoxDownloadWifiOnly.isChecked());
				editor.putBoolean(Common.SP_ALBUM_ART_IS_DOWNLOAD_HIGH_QUALITY,
						mChkBoxDownloadHighQuality.isChecked());
				editor.commit();

				if (mChkBoxAutoDownload.isChecked()) {
					// enable other options
					updateLayoutWhenAutoDownChanged(true);
					mChkBoxDownloadWifiOnly.setEnabled(true);
					mChkBoxDownloadHighQuality.setEnabled(true);

					// download all album art
					if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD) {
						new AlbumArtGettingTask(getApplicationContext())
								.executeOnExecutor(
										AsyncTask.THREAD_POOL_EXECUTOR, -1);
					} else {
						new AlbumArtGettingTask(getApplicationContext())
								.execute(-1);
					}
				} else {
					// disable other options
					updateLayoutWhenAutoDownChanged(false);
					mChkBoxDownloadWifiOnly.setEnabled(false);
					mChkBoxDownloadHighQuality.setEnabled(false);

				}
			}
		});

		mChkBoxDownloadWifiOnly.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting value
				SharedPreferences sp = getSharedPreferences(
						Common.SP_ALBUM_ART_SETTING_NAME, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = sp.edit();
				editor.putBoolean(Common.SP_ALBUM_ART_IS_DOWNLOAD_WIFI_ONLY,
						mChkBoxDownloadWifiOnly.isChecked());
				editor.commit();
				if (mChkBoxDownloadWifiOnly.isChecked()) {
					setAlpha(
							((LinearLayout) findViewById(R.id.layout_setting_album_art_download_on_wifi_only)),
							1.0f);
				} else {
					setAlpha(
							((LinearLayout) findViewById(R.id.layout_setting_album_art_download_on_wifi_only)),
							0.5f);
				}

			}
		});

		mChkBoxDownloadHighQuality.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting value
				SharedPreferences sp = getSharedPreferences(
						Common.SP_ALBUM_ART_SETTING_NAME, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = sp.edit();
				editor.putBoolean(Common.SP_ALBUM_ART_IS_DOWNLOAD_HIGH_QUALITY,
						mChkBoxDownloadHighQuality.isChecked());
				editor.commit();
				if (mChkBoxDownloadHighQuality.isChecked()) {
					setAlpha(
							((LinearLayout) findViewById(R.id.layout_setting_album_art_download_high_quality)),
							1.0f);
				} else {
					setAlpha(
							((LinearLayout) findViewById(R.id.layout_setting_album_art_download_high_quality)),
							0.5f);
				}
			}
		});

		mChkBoxDisplayDisplayAlbum.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting value
				SharedPreferences sp = getSharedPreferences(
						Common.SP_ALBUM_ART_SETTING_NAME, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = sp.edit();
				editor.putBoolean(Common.SP_ALBUM_ART_IS_DISPLAY_ALBUM_ART,
						mChkBoxDisplayDisplayAlbum.isChecked());
				editor.commit();
				// if (mChkBoxDisplayDisplayAlbum.isChecked()) {
				// setAlpha(
				// ((LinearLayout)
				// findViewById(R.id.layout_setting_album_art_download_high_quality)),
				// 1.0f);
				// } else {
				// setAlpha(
				// ((LinearLayout)
				// findViewById(R.id.layout_setting_album_art_download_high_quality)),
				// 0.5f);
				// }
			}
		});
	}
}
